package com.action.user;

import java.io.File;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import com.common.ImageProcess;
import com.db.organization.DeptJobDAO;
import com.db.user.*;
public class UserInputAction extends ActionSupport implements ModelDriven<Object>, Preparable, ServletRequestAware{
	// 파일 업로드 관련
	private File upload;
	private File saveFile;
	private String uploadFileName;	
	private HttpServletRequest servletRequest;

	// 기타 유저정보
	private UserVO uVO;	
	private UserDAO userDAO;
	private DeptJobDAO orgDAO;
	private ImageProcess imgProc;
	
	public void setImgProc(ImageProcess imgProc) {
		this.imgProc = imgProc;
	}
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
	public void setOrgDAO(DeptJobDAO orgDAO) {
		this.orgDAO = orgDAO;
	}
	
	public void setuVO(UserVO uVO) {
		this.uVO = uVO;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	@Override
	public void prepare() throws Exception {
		uVO = new UserVO();
	}
	@Override
	public Object getModel() {
		return uVO;
	}
	
	// 업로드 경로를 얻기 위해 리퀘스트 객체를 얻어온다
	@Override
	public void setServletRequest(HttpServletRequest servletRequest) {
		this.servletRequest = servletRequest;
	}
	
	public String insertUser(){
		String result = "";
		try {
			uVO.setSlevel(orgDAO.getSlevel(uVO.getJobno()));
			userDAO.insertUser(uVO);			
			result = "success";
		} catch (Exception e) {
			System.out.println("UserInputAction.insertUser():"+e.toString());
			result = "error";
		}
		return result;
	}
	
	public String updateUser(){
		String result = "";	
		String fn = "";
		try {
			// 실제 업로드 대상 폴더의 경로를 얻어온다			
			String path = servletRequest.getSession().getServletContext().getRealPath("/upload/user/photo");		
			// 업로드 사진이 있을 경우 파일을 생성한다
			if(upload != null && upload.exists()){				
				//saveFile = new File(path+"\\"+uploadFileName);
				fn = "pi_"+uVO.getSabun()+".jpg";
				saveFile = new File(path+"\\"+fn);
				FileUtils.copyFile(upload, saveFile);				
				imgProc.createFixedSize(path, fn);
				// 파일명을 vo에 저장
				uVO.setPhotofn(fn);
			}
			// 직급 테이블로부터 얻어온 보안등급 초기값 설정			
			uVO.setSlevel(orgDAO.getSlevel(uVO.getJobno()));
			// 실제 업데이트 수행
			userDAO.updateUser(uVO);
			// 수정된 vo객체를 세션에 저장
			ActionContext context = ActionContext.getContext();
			Map<String, UserVO> session = (Map<String, UserVO>)context.getSession();
			uVO = userDAO.getUserInfo(uVO.getSabun());
			session.put("user", uVO);				
			context.setSession(session);			
			result = "success";
		} catch (Exception e) {
			System.out.println("UserInputAction.updateUser():"+e.toString());			
			result = "error";
		}
		return result;
	}
}
